package com.jstyle.devicenametest.ble;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import java.util.List;

/**
 * Created by Administrator on 2017/4/11.
 */

public class BleManager {
    private static final String TAG = "BleManager";
    private static BleManager ourInstance;
    private String address;
    private BleService bleService;
    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub
            bleService = null;
            Log.i(TAG, "onServiceDisconnected: ");
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            bleService = ((BleService.LocalBinder) service).getService();
            Log.i(TAG, "onServiceConnected: ");
            if (!TextUtils.isEmpty(address)) {
                bleService.initBluetoothDevice(address, context);
            }
        }
    };
    private Intent serviceIntent;
    BluetoothAdapter bluetoothAdapter;
    Context context;

    private BleManager(Context context) {
        this.context = context;
        if (serviceIntent == null) {
            Log.i(TAG, "BleManager: bindService");
            serviceIntent = new Intent(context, BleService.class);
            context.bindService(serviceIntent, serviceConnection,
                    Service.BIND_AUTO_CREATE);
        }
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();
    }

    public static void init(Context context) {
        if (ourInstance == null) {
            synchronized (BleManager.class) {
                if (ourInstance == null) {
                    ourInstance = new BleManager(context);
                }
            }
        }
    }

    public boolean isBleEnable() {
        return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
    }

    public static BleManager getInstance() {

        return ourInstance;
    }

    public void connectDevice(String address) {
        if (bleService == null) {
            this.address = address;
        } else {
            bleService.initBluetoothDevice(address, this.context);
        }
    }

    public void connectDevice(String address, boolean reConnect) {
        if (!bluetoothAdapter.isEnabled() || TextUtils.isEmpty(address) || isConnected()) return;

        if (bleService == null) {
            this.address = address;
        } else {
            bleService.NeedReconnect = reConnect;
            bleService.initBluetoothDevice(address, this.context);
        }
    }

    public void changeDevice(String address, boolean reConnect) {
        if (!bluetoothAdapter.isEnabled() || TextUtils.isEmpty(address) || isConnected()) return;

        this.address = address;
        if (bleService == null) {
        } else {
            bleService.NeedReconnect = reConnect;
            bleService.changeBluetoothDevice(address);
        }
    }


    public void enableNotifaction() {
        if (bleService == null) return;
        bleService.setCharacteristicNotification(true);
    }

    public void writeValue(byte[] value) {
        if (bleService == null || ourInstance == null || !isConnected()) return;
        bleService.writeValue(value);
    }

    public void offerValue(byte[] data) {
        if (bleService == null) return;
        bleService.offerValue(data);
    }

    public void offerValue(List<byte[]> list) {
        if (bleService == null) return;
        bleService.offerValue(list);
    }

    public void writeValue() {
        if (bleService == null) return;
        bleService.nextQueue();
    }

    public void disconnectDevice() {
        if (bleService == null) return;
        //  bleService.setDisConnected();
        bleService.disconnect();
    }

    public void disconnectDeviceAndClose() {
        if (bleService == null) return;
        bleService.setDisConnected();
        //   bleService.disconnect();
    }

    public boolean isConnected() {
        if (bleService == null) return false;

        return bleService.isConnected();
    }

    public void unbindService() {
        if (bleService == null) return;
        context.unbindService(serviceConnection);
        bleService = null;
    }

    public void reConnect() {
        if (bleService == null) return;
        bleService.startScan(true);
    }

    public String getAddress() {
        return address;
    }

    public void disconnectUnPair(String address) {
        if (bleService == null) return;
        bleService.disconnectUnPairDevice(address);
    }
}
